package com.jsnkj.basic.util

import com.jsnkj.basic.advertisement.AdvertisementManager
import com.jsnkj.basic.bean.AdPostTypeBean
import com.jsnkj.basic.bean.AdSettingListBean
import com.jsnkj.basic.bean.AdTypesEntity
import com.jsnkj.basic.config.RewardsConfig
import com.jsnkj.basic.config.SpConfig
import java.lang.Exception

object RewardsUtil {

    //广告数据
    var adTypeList: ArrayList<AdTypesEntity> = ArrayList()

    //获取短视频广告比例
    fun getRewardRate(position: String): Float {
        try {
            val adTypesEntity = getAdTypesEntity(position)
            if (null != adTypesEntity) {
                return adTypesEntity.reward_rate!!.toFloat()
            }
        } catch (e: Exception) {
            return RewardsConfig.getAdScale
        }
        return RewardsConfig.getAdScale
    }


    fun getAdTypesEntity(position: String): AdTypesEntity? {
        if (adTypeList.isNullOrEmpty()) {
            return null
        }

        for (entity in adTypeList) {
            if (position == entity.position) {
                return entity
            }
        }
        return null
    }


    fun addAdTypesEntity(list: ArrayList<AdTypesEntity>) {
        adTypeList = ArrayList()
        adTypeList.addAll(list)
    }

    fun addAdTypeRewardGoldNumber(adType: String){
        var ecpm:Int = AdvertisementManager.getAdSettingListBean(adType)!!.reward_num as Int
        addGoldNumber(ecpm,adType);
    }


    //ECPM
    fun addGoldNumber(ecpm: Int, position: String) {
        if (ecpm == 0) {
            return
        }
        val goldNumber = getRewardRate(position) * (ecpm / SpConfig.ecpmCoinConversion) *
                SharePreferencesUtil.getInstance().getSettingEntity().exchange_rate
        com.jsnkj.basic.util.LogUtils.e(
            "广告类型：position == $position  ecpm = $ecpm   " +
                    "getRewardRate(position) == ${getRewardRate(position)}" +
                    " 添加金币：   goldNumber=$goldNumber"
        )
        //添加广告数据
        getAdSettingEntity(position)?.let { listBean ->
            listBean.adtype?.let { bean ->
                val adBean = AdPostTypeBean.getInstance(
                    goldNumber.toInt().toString(),
                    ecpm.toString(),
                    bean.id.toString(),
                    bean.name.toString(),
                    listBean.id.toString(),
                    listBean.value.toString(),
                    listBean.reward_rate,
                    SharePreferencesUtil.getInstance().getSettingEntity().exchange_rate.toString(),
                    (ecpm / SpConfig.ecpmCoinConversion).toString(),
                    System.currentTimeMillis().toString()
                )
                SharePreferencesUtil.getInstance().putAdPostTypeBean(adBean)
            }
        }
    }


    //获取广告设置的数据
    fun getAdSettingEntity(position: String): AdSettingListBean? {
        try {
            val adSettingEntity = SharePreferencesUtil.getInstance().getAdSettingEntity()
            if (adSettingEntity.list.isNullOrEmpty()) {
                return null
            }
            for (entity in adSettingEntity.list!!) {
                if (position == entity.position) {
                    return entity
                }
            }

        } catch (e: Exception) {
            return null
        }
        return null
    }


}

























